﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using com.tc.zigbee.ui;
using com.tc.zigbee.mt;
using com.tc.util;
using com.tc.assistance.engine;
using com.tc.gateway.gevent;
using com.tc.zigbee.ha;
using com.tc.zigbee.entity;
using com.tc.assistance.home;
using com.tc.assistance.entity.device;
namespace MyQQ
{
    public partial class HomeConsole : Form,  ServerListener, EventListener
    {
        AbstractController controller;
        HomeApplication application;
        public HomeConsole(HomeApplication app, AbstractController c)
        {
            application = app;
            controller = c;

            InitializeComponent();

            controller.addServerListener(this);
            System.Reflection.Assembly a = System.Reflection.Assembly.Load("GatewayAbstraction");   

            Type t = a.GetType("com.tc.gateway.gevent.RefreshEvent");
            EventCenter.getInstance().register(this, t); 
        }

        private void HomeConsole_Load(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;  

            CreateTree();
        }
        private void CreateTree()
        {
            TreeNode node = new TreeNode("家居设备");
            node.SelectedImageKey = "assistance";
            node.ImageKey = "assistance";
           
            this.deviceTree.Nodes.Add(node);

           List<Endpoint> gg =  application.getManager().getControlledEndpointsProfile(HAConstants.ZCL_HA_PROFILE_ID);
           foreach (Endpoint e in gg)
           {
               TreeNode d  = new TreeNode(e.tostring());
               string dy = "d" + DataUtil.hex(e.getProfileId()) + DataUtil.hex(e.getDeviceId());
               d.SelectedImageKey = dy;
               d.ImageKey = dy;
               d.Tag = e.getDevice();
               node.Nodes.Add(d);
           }
           deviceTree.ExpandAll();
            //node.Tag = 
        }

        public void Update()
        {
            this.deviceTree.Nodes.Clear();
            CreateTree();
        }
        public void onRelationDownloaded(DetailedRelation dr, Dictionary<string, GeneralDevice> devices)
        {
           
            foreach (Relationship u in dr.outd)
            {
           //     SbItem item = new SbItem(u.ToString(), 0);

             //   sbFriend.Groups[0].Items.Add(item);
            }
        }
        public void onDefinationUploaded()
        {
        }
        delegate void UpdateDelegate();
        public void onEvent(Event e)

        {
            this.Invoke(new UpdateDelegate(this.Update));
        }
    }
   
}
